﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace devtm.Configuration
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1046:DoNotOverloadOperatorEqualsOnReferenceTypes"), AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
    public sealed class RequiredVersionEditorAttribute : Attribute
    {

        private Version version;

        #region ctors

        ///     <summary>
        ///     Initializes a new instance of the <see cref="RequiredVersionEditorAttribute"/> class.
        ///     </summary>
        ///     <param name="major">The major.</param>
        ///     <param name="minor">The minor.</param>
        public RequiredVersionEditorAttribute(int major, int minor)
        {
            version = new Version(major, minor);
        }


        ///     <summary>
        ///     Initializes a new instance of the <see cref="RequiredVersionEditorAttribute"/> class.
        ///     </summary>
        ///     <param name="major">The major.</param>
        ///     <param name="minor">The minor.</param>
        ///     <param name="build">The build.</param>
        public RequiredVersionEditorAttribute(int major, int minor, int build)
        {
            version = new Version(major, minor, build);
        }

        #endregion

        public override bool Equals(object obj)
        {
            return version.Equals(obj);
        }

        public override int GetHashCode()
        {
            return version.GetHashCode();
        }

        #region ToString

        ///     <summary>
        ///     Converts the value of the current RequiredVersionAttribute object to its equivalent
        ///     System.String representation.
        ///     </summary>
        ///     <returns>
        ///     The System.String representation of the values of the major, minor, build,
        ///     and revision components of the current RequiredVersionAttribute object, as depicted
        ///     in the following format. Each component is separated by a period character
        ///     ('.'). Square brackets ('[' and ']') indicate a component that will not appear
        ///     in the return value if the component is not defined: major.minor[.build[.revision]]
        ///     For example, if you create a RequiredVersionAttribute object using the constructor
        ///     Version(1,1), the returned string is "1.1". If you create a RequiredVersionAttribute
        ///     object using the constructor Version(1,3,4,2), the returned string is "1.3.4.2".
        ///     </returns>
        public override string ToString()
        {
            return version.ToString();
        }


        ///      <summary></summary>
        ///      Converts the value of the current RequiredVersionAttribute object to its equivalent
        ///      System.String representation. A specified count indicates the number of components
        ///      to return.
        ///      </summary>
        ///      <param name="fieldCount">The number of components to return. The fieldCount ranges from 0 to 4.</param>
        ///      <returns>
        ///      The System.String representation of the values of the major, minor, build,
        ///      and revision components of the current RequiredVersionAttribute object, each separated
        ///      by a period character ('.'). The fieldCount parameter determines how many
        ///      components are returned.  fieldCount Return Value 0 An empty string ("").
        ///      1 major 2 major.minor 3 major.minor.build 4 major.minor.build.revision For
        ///      example, if you create RequiredVersionAttribute object using the constructor Version(1,3,5),
        ///      ToString(2) returns "1.3" and ToString(4) throws an exception.
        ///      </returns>
        ///      <exceptions>
        ///      System.ArgumentException:
        ///      fieldCount is less than 0, or more than 4.  -or- fieldCount is more than
        ///      the number of components defined in the current RequiredVersionAttribute object.
        public string ToString(int fieldCount)
        {
            return version.ToString(fieldCount);
        }

        #endregion

        #region Properties

        ///     <summary>
        ///     Gets the value of the build component of the version number for the current
        ///     RequiredVersionAttribute object.
        ///     </summary>
        ///     <returns>
        ///     The build number, or -1 if the build number is undefined.
        ///     </returns>
        public int Build { get { return version.Build; } }

        ///     <summary>
        ///     Gets the value of the major component of the version number for the current
        ///     RequiredVersionAttribute object.
        ///     </summary>
        ///     <returns>
        ///     The major version number.
        ///      </returns>
        public int Major { get { return version.Major; } }

        ///     <summary>
        ///     Gets the high 16 bits of the revision number.
        ///     </summary>
        ///     <returns>
        ///     A 16-bit signed integer.
        ///     </returns>
        public short MajorRevision { get { return version.MajorRevision; } }

        ///     <summary>
        ///     Gets the value of the minor component of the version number for the current
        ///     RequiredVersionAttribute object.
        ///     </summary>
        ///     <returns>
        ///     The minor version number.
        ///  </returns>
        public int Minor { get { return version.Minor; } }

        ///     <summary>
        ///     Gets the low 16 bits of the revision number.
        ///     </summary>
        ///     <returns>
        ///     A 16-bit signed integer.
        ///     </returns>
        public short MinorRevision { get { return version.MinorRevision; } }

        ///     <summary>
        ///     Gets the value of the revision component of the version number for the current
        ///     RequiredVersionAttribute object.
        ///     </summary>
        ///     <returns>
        ///     The revision number, or -1 if the revision number is undefined.
        ///     </returns>
        public int Revision { get { return version.Revision; } }

        #endregion

        #region operators

        ///     <summary>
        ///     Determines whether two specified RequiredVersionAttribute objects are not equal.
        ///     </summary>
        ///     <param name="v1">The first RequiredVersionAttribute object.</param>
        ///     <param name="v2">The second RequiredVersionAttribute object.</param>
        ///     <returns>
        ///     true if v1 does not equal v2; otherwise, false.
        ///     </returns>        
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2225:OperatorOverloadsHaveNamedAlternates")]
        public static bool operator !=(RequiredVersionEditorAttribute v1, RequiredVersionEditorAttribute v2)
        {
            if (v1 == null)
                throw new ArgumentNullException("v1");

            if (v2 == null)
                throw new ArgumentNullException("v2");

            return (v1.version != v2.version);
        }


        ///     <summary>
        ///     Determines whether the first specified RequiredVersionAttribute object is less than
        ///     the second specified RequiredVersionAttribute object.
        ///     </summary>
        ///     <param name="v1">The first RequiredVersionAttribute object.</param>
        ///     <param name="v2">The second RequiredVersionAttribute object.</param>
        ///     <returns>
        ///     true if v1 is less than v2; otherwise, false.
        ///     </returns>
        ///     <exceptions>
        ///     <see cref="System.ArgumentNullException"/>
        ///     v1 is null.
        ///     </exceptions>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2225:OperatorOverloadsHaveNamedAlternates")]
        public static bool operator <(RequiredVersionEditorAttribute v1, RequiredVersionEditorAttribute v2)
        {
            if (v1 == null)
                throw new ArgumentNullException("v1");

            if (v2 == null)
                throw new ArgumentNullException("v2");

            return (v1.version < v2.version);
        }


        ///     <summary>
        ///     Determines whether the first specified RequiredVersionAttribute object is less than
        ///     or equal to the second RequiredVersionAttribute object.
        ///     </summary>
        ///     <param name="v1">The first RequiredVersionAttribute object.</param>
        ///     <param name="v2">The second RequiredVersionAttribute object.</param>
        ///     <returns>
        ///     true if v1 is less than or equal to v2; otherwise, false.
        ///     </returns>
        ///     <exceptions>
        ///     <see cref="System.ArgumentNullException"/>
        ///     v1 is null.
        ///     </exceptions>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2225:OperatorOverloadsHaveNamedAlternates")]
        public static bool operator <=(RequiredVersionEditorAttribute v1, RequiredVersionEditorAttribute v2)
        {
            if (v1 == null)
                throw new ArgumentNullException("v1");

            if (v2 == null)
                throw new ArgumentNullException("v2");

            return (v1.version <= v2.version);
        }


        ///     <summary>
        ///     Determines whether two specified RequiredVersionAttribute objects are equal.
        ///     </summary>
        ///     <param name="v1">The first RequiredVersionAttribute object.</param>
        ///     <param name="v2">The second RequiredVersionAttribute object.</param>
        ///     <returns>
        ///     true if v1 equals v2; otherwise, false.
        ///     </returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2225:OperatorOverloadsHaveNamedAlternates")]
        public static bool operator ==(RequiredVersionEditorAttribute v1, RequiredVersionEditorAttribute v2)
        {
            if (v1 == null && v2 == null)
                return true;
            if (v1 == null || v2 == null)
                return false;

            return (v1.version == v2.version);
        }


        ///     <summary>
        ///     Determines whether the first specified RequiredVersionAttribute object is greater than
        ///     the second specified RequiredVersionAttribute object.
        ///     </summary>
        ///     <param name="v1">The first RequiredVersionAttribute object.</param>
        ///     <param name="v2">The second RequiredVersionAttribute object.</param>
        ///     <returns>
        ///     true if v1 is greater than v2; otherwise, false.
        ///     </returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2225:OperatorOverloadsHaveNamedAlternates")]
        public static bool operator >(RequiredVersionEditorAttribute v1, RequiredVersionEditorAttribute v2)
        {
            if (v1 == null && v2 == null)
                return false;
            if (v1 == null)
                return false;
            if (v2 == null)
                return true;

            return (v1.version > v2.version);
        }


        ///     <summary>
        ///     Determines whether the first specified RequiredVersionAttribute object is greater than
        ///     or equal to the second specified RequiredVersionAttribute object.
        ///     </summary>
        ///     <param name="v1">The first RequiredVersionAttribute object.</param>
        ///     <param name="v2">The second RequiredVersionAttribute object.</param>
        ///     <returns>
        ///     true if v1 is greater than or equal to v2; otherwise, false.
        ///     </returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2225:OperatorOverloadsHaveNamedAlternates")]
        public static bool operator >=(RequiredVersionEditorAttribute v1, RequiredVersionEditorAttribute v2)
        {
            if (v1 == null && v2 == null)
                return true;
            if (v1 == null)

                return false;
            if (v2 == null)
                return true;

            return (v1.version >= v2.version);
        }


        #endregion

    }
}
